#include <vector>
#include <iostream>
#include <cmath>

using namespace std;

struct ListNode
{
    int val;
    ListNode * next;
    ListNode() : val(0), next(nullptr)
    {}
    ListNode(int val) : val(val), next(nullptr)
    {}
    ListNode(int val, ListNode * next) : val(val), next(next)
    {}
};

class Solution 
{
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode * _dummyHead = new ListNode(0, head);
        ListNode * fast = _dummyHead;
        ListNode * slow = _dummyHead;
        for(int i = 0; i < n; i++)
        {
            fast = fast->next;
        }
        while(fast != nullptr && fast->next != nullptr)
        {
            slow = slow->next;
            fast = fast->next;
        }
        ListNode * toDelete = slow->next;
        slow->next = slow->next->next;
        delete toDelete;
        ListNode * result = _dummyHead->next;
        delete _dummyHead;
        return result;
    }
};

// 辅助函数：从vector创建链表
ListNode* createList(vector<int>& nums) {
    if (nums.empty()) return nullptr;
    
    ListNode* head = new ListNode(nums[0]);
    ListNode* cur = head;
    
    for (int i = 1; i < nums.size(); i++) {
        cur->next = new ListNode(nums[i]);
        cur = cur->next;
    }
    
    return head;
}

// 辅助函数：打印链表
void printList(ListNode* head) {
    ListNode* cur = head;
    cout << "[";
    while (cur != nullptr) {
        cout << cur->val;
        if (cur->next != nullptr) cout << ",";
        cur = cur->next;
    }
    cout << "]";
}

// 辅助函数：释放链表内存
void deleteList(ListNode* head) {
    while (head != nullptr) {
        ListNode* temp = head;
        head = head->next;
        delete temp;
    }
}

int main() {
    Solution solution;
    
    // 测试案例1：题目示例1 [1,2,3,4,5], n = 2
    cout << "测试案例1: ";
    vector<int> nums1 = {1, 2, 3, 4, 5};
    int n1 = 2;
    ListNode* head1 = createList(nums1);
    cout << "head = ";
    printList(head1);
    cout << ", n = " << n1;
    ListNode* result1 = solution.removeNthFromEnd(head1, n1);
    cout << " -> ";
    printList(result1);
    cout << " (期望: [1,2,3,5])" << endl;
    deleteList(result1);
    
    // 测试案例2：题目示例2 [1], n = 1
    cout << "测试案例2: ";
    vector<int> nums2 = {1};
    int n2 = 1;
    ListNode* head2 = createList(nums2);
    cout << "head = ";
    printList(head2);
    cout << ", n = " << n2;
    ListNode* result2 = solution.removeNthFromEnd(head2, n2);
    cout << " -> ";
    printList(result2);
    cout << " (期望: [])" << endl;
    deleteList(result2);
    
    // 测试案例3：题目示例3 [1,2], n = 1
    cout << "测试案例3: ";
    vector<int> nums3 = {1, 2};
    int n3 = 1;
    ListNode* head3 = createList(nums3);
    cout << "head = ";
    printList(head3);
    cout << ", n = " << n3;
    ListNode* result3 = solution.removeNthFromEnd(head3, n3);
    cout << " -> ";
    printList(result3);
    cout << " (期望: [1])" << endl;
    deleteList(result3);
    
    // 测试案例4：删除第一个节点 [1,2,3], n = 3
    cout << "测试案例4: ";
    vector<int> nums4 = {1, 2, 3};
    int n4 = 3;
    ListNode* head4 = createList(nums4);
    cout << "head = ";
    printList(head4);
    cout << ", n = " << n4;
    ListNode* result4 = solution.removeNthFromEnd(head4, n4);
    cout << " -> ";
    printList(result4);
    cout << " (期望: [2,3])" << endl;
    deleteList(result4);
    
    // 测试案例5：删除中间节点 [1,2,3,4,5], n = 3
    cout << "测试案例5: ";
    vector<int> nums5 = {1, 2, 3, 4, 5};
    int n5 = 3;
    ListNode* head5 = createList(nums5);
    cout << "head = ";
    printList(head5);
    cout << ", n = " << n5;
    ListNode* result5 = solution.removeNthFromEnd(head5, n5);
    cout << " -> ";
    printList(result5);
    cout << " (期望: [1,2,4,5])" << endl;
    deleteList(result5);
    
    // 测试案例6：两个节点删除第二个 [1,2], n = 2
    cout << "测试案例6: ";
    vector<int> nums6 = {1, 2};
    int n6 = 2;
    ListNode* head6 = createList(nums6);
    cout << "head = ";
    printList(head6);
    cout << ", n = " << n6;
    ListNode* result6 = solution.removeNthFromEnd(head6, n6);
    cout << " -> ";
    printList(result6);
    cout << " (期望: [2])" << endl;
    deleteList(result6);
    
    // 测试案例7：长链表删除倒数第一个 [1,2,3,4,5,6,7], n = 1
    cout << "测试案例7: ";
    vector<int> nums7 = {1, 2, 3, 4, 5, 6, 7};
    int n7 = 1;
    ListNode* head7 = createList(nums7);
    cout << "head = ";
    printList(head7);
    cout << ", n = " << n7;
    ListNode* result7 = solution.removeNthFromEnd(head7, n7);
    cout << " -> ";
    printList(result7);
    cout << " (期望: [1,2,3,4,5,6])" << endl;
    deleteList(result7);
    
    return 0;
}